package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealConteoller {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     *
     添加 setmeal表 与setmeal_dish
     扩展dishDto表
     * @param setmealDto
     * @return
     * 添加之后删除redis 中的缓存
     */
    @CacheEvict(value = "setmeal", key = "#setmealDto.categoryId + '_' + #setmealDto.status")
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("传递回来的数据dishDto:{}",setmealDto);
        //代码过多转到service层
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }


    /**
     *
     * 多表查询
        Stemeal 与 category表
     setmeal中catgoryid=XXX

     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name){
        Page setmealDtoPage=setmealService.pageWithCategory(page,pageSize,name);
        return R.success(setmealDtoPage);
    }

    /**
     *删除
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmeal", allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @Cacheable(value = "setmeal", key = "id + '_1'")
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto =setmealService.setmealWithCate(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmeal", key = "#setmealDto.categoryId + '_' + #setmealDto.status")
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithSetmeal(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 套餐起售
     * 根据传回来的Id修改套餐的状态
     *
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmeal", allEntries = true)
    @PostMapping("status/{status}")
    public R<String> status1(@RequestParam List<Long> ids,@PathVariable Integer status){

        //创建setmeal（套餐）对象
        Setmeal setmeal = new Setmeal();
        LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<>();
        setmealLqw.in(Setmeal::getId,ids);
        setmeal.setStatus(status);
        //调用方法修改
        setmealService.update(setmeal,setmealLqw);
        return R.success("状态修改成功");
    }

    /**
     * 查询商务套餐
     * 根据前端传回的id 查询 并且状态为1
     * 给前端返回
     * key = 传回的id 状态
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmeal", key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<>();
        //查询id与状态
        setmealLqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId()).
                    eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        //根据修改时间排序
        setmealLqw.orderByDesc(Setmeal::getUpdateTime);
        //调用方法查询
        List<Setmeal> list = setmealService.list(setmealLqw);
        return R.success(list);
    }

    /**
     *根据传回来的 SetmealId查询菜品
     * 由于setmealdish缺少图片
     * 查询dish表拿到图片
     * 将菜品 与图片 返回给前端
     *
     * @param
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<List<DishDto>> dish(@PathVariable  Long id){

        //根据传回来的 SetmealId查询菜品
        LambdaQueryWrapper<SetmealDish> setDishLqw = new LambdaQueryWrapper<>();
        setDishLqw.eq(id != null, SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(setDishLqw);

        //查询dish表拿到图片
        List<DishDto> dtoList =setmealDishList.stream().map((setmealDishItem) ->{

            //创建dishDto 对象
            DishDto dishDto = new DishDto();
            //将setmealDish集合查出来的数据复制到dishDto中
            BeanUtils.copyProperties(setmealDishItem, dishDto);

            //根据setmeal中的dishId查询dish表
            LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<>();
            dishLqw.eq(setmealDishItem.getDishId() != null, Dish::getId, setmealDishItem.getDishId());
            Dish dish = dishService.getOne(dishLqw);

            dishDto.setImage(dish.getImage());
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dtoList);
    }
}
